<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <hr>
        <el-tree default-expand-all :data="departs" :props="defaultProps">
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @addDepts="addDepts"
            @delDepts="getDepartments"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>

      <AddDept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :node="node"
        @addDepts="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tools'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      showDialog: false,
      node: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      this.departs = transListToTreeData(res.depts, '')
      console.log(res)
    },
    addDepts(treeNode) {
      console.log('子组件点击新增触发,获取到被点击的那个组件的数据')
      console.log(treeNode)
      // 1. 控制弹窗
      this.showDialog = true
      // 2. 将获取的 treeNode 传给弹窗
      this.node = treeNode
    },
    async editDepts(treeNode) {
      // 因为是编辑, 除了弹窗以外, 还需要执行子组件获取数据方法
      // 来实现数据回显
      await this.$refs.addDept.getDepartDetail(treeNode.id)
      this.showDialog = true
      this.node = treeNode
    }
  }
}
</script>

<style scoped>
  .tree-card {
    padding: 30px 140px;
    font-size: 14px;
  }
</style>
